// ==UserScript==
// @name         自动录入脚本
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       ReStartLin
// @match        http://www.kulemi.com/edit/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Your code here...
    console.log("tampermonkey start");
    var a =  $(".container");
    var id = 0;
    var addBtn = "<div class='container'><button id='myaddBtn' class='btn'>添加</button><button id='mysuccessBtn' class='btn'>记录</button><button id='myclearBtn' class='btn'>舍弃</button></div>";
    a.after(addBtn);
    $("#myaddBtn").click(function(){
        console.log('添加按钮被点击');
        var name = $('span').filter(".tname");
        var gameName = name[0].innerHTML;
        console.log('游戏名字是:',gameName);
        console.log("搜索开始...");
        $.ajax({
            url:"http://localhost/api/info/game/"+gameName,
            type:"GET",
            processData:false,
            contentType:false,
            success:function(data){
                console.log("搜索结束");
                console.log(data);
                if(data.error == false){
                   var info = data.data;
                   id = data.data.id;
                    //脚本注入
                   $("#classid").val(2);
                   $("#classid").trigger("change");
                   $("#title").val(info.title);
                   var edt = $("#editor");
                   edt[0].innerHTML = info.content;
                }else{
                   console.log("发生错误");
                   console.log(data.msg);
                }
            },
            fail:function(error){
                console.log("发生错误");
                console.log(error);
            }
        });
    });
    $("#mysuccessBtn").click(function(){
        console.log('记录按钮被点击');
        console.log("请求开始");
         $.ajax({
            url:"http://localhost/api/info/success/"+id,
            type:"GET",
            processData:false,
            contentType:false,
            success:function(data){
                console.log("请求结束");
                console.log(data);
                $("#sclassid").val(9);
                $("#sclassid").trigger("change");
            },
            fail:function(error){
                console.log("发生错误");
                console.log(error);
            }
        });
    });
     $("#myclearBtn").click(function(){
        console.log('舍弃按钮被点击');
        console.log("请求开始");
         $.ajax({
            url:"http://localhost/api/info/fail/"+id,
            type:"GET",
            processData:false,
            contentType:false,
            success:function(data){
                console.log("请求结束");
                console.log(data);
                $("#sclassid").val(9);
                $("#sclassid").trigger("change");
            },
            fail:function(error){
                console.log("发生错误");
                console.log(error);
            }
        });
    });
})();